<?php

class Model_Themes extends RL_Model2 {
  private $lpage = 1;
  private $pages = 0;
  private $per_page = 5;
  private $usePages = true;
  
	public function setLPage( $lpage = 0 ) {
		$this->lpage = abs($lpage+0);
		if ( $this->lpage <= 0 ) {
			$this->lpage = 1;
		}
	}

	public function getLPage() {
		return $this->lpage;
	}
  
  public function getPages() {
    return $this->pages;
  }
  
  public function setDisablePages() {
    $this->usePages = false;
  }
  
  public function getThemes( $community_oid = 0 ) {
    $themes = array();
    if ( 0 == $community_oid && !empty($this->mm->aa->acl_data['comity_oid']) ) {
      $community_oid = (integer) $this->mm->aa->acl_data['comity_oid'];
    }
    
    $sql = "select count(*) as count " .
    " from {$this->store->db->db_prefix}class_theme t " .
    " left join {$this->store->db->db_prefix}objects o on o.oid = t.oid " .
    "where o.is_deleted = 0 and t.community_oid = {$community_oid}";
    
    $result = $this->store->query($sql);
		if ( !($result instanceof MysqlDBResult) ) {
			return array();
		}
    
    $row = $result->next();
    if ( !isset($row['count']) || empty($row['count']) ) {
      return array();
    }
    
    $count = $row['count'];
    if ( $this->usePages ) {
      $this->pages = (int) ($count / $this->per_page);
  		if ( $count % $this->per_page > 0 ) {
  			$this->pages++;
  		}
  		if ( $this->lpage > $this->pages ) {
  			$this->lpage = $this->pages;
  		}
      $from = ($this->lpage - 1) * $this->per_page;
    } else {
      $this->pages = 1;
      $this->lpage = 1;
    }
    
    $sql = "select t.oid from {$this->store->db->db_prefix}class_theme t " .
    " left join {$this->store->db->db_prefix}objects o on o.oid = t.oid " .
    "where o.is_deleted = 0 and t.community_oid = {$community_oid} order by t.oid ";
    if ( $this->usePages ) {
      $sql .= "limit {$from}, {$this->per_page}";
    }
    $result = $this->store->query($sql);

		if ( !($result instanceof MysqlDBResult) ) {
			return array();
		}

    while ( $row = $result->next() ) {
      $object = $this->store->getObject($row['oid']);
      $object->data['oid'] = $row['oid'];
      
      $parent = $this->store->getObject($object->data['parent_oid']);
      $parent->data['type'] = $parent->type;
      
      $object->data['parent'] = $parent->data;
      
      $themes[] = $object->data;
    }
    
    return $themes;
  }
  
  public function getFilters( $oid, $withFilterPattern = false ) {
    $sql = "select f.oid from {$this->store->db->db_prefix}class_filter f " .
    " left join {$this->store->db->db_prefix}objects o on o.oid = f.oid " .
    " where o.is_deleted = 0 and f.theme_oid = {$oid} order by f.oid";
    $result = $this->store->query($sql);

		if ( !($result instanceof MysqlDBResult) ) {
			return array();
		}
    
    $filters = array();
    if ( $withFilterPattern ) {
      $this->mm->rh->useClass("Filters_List");
      $filtersList = new Filters_List ( $this->mm->rh->ctx );
    }
    while ( $row = $result->next() ) {
      $object = $this->store->getObject($row['oid']);
      if ( $withFilterPattern ) {
        $filter = $filtersList->getFilterObject( $object->data['filter'] );
        $object->data['filter_pattern'] = $filter->getFilterPattern( $row['oid'] );
      }
      $object->data['oid'] = $row['oid'];
      $filters[] = $object->data;
    }
    
    return $filters;
  }
}